package com.example.componentstool;

import android.Manifest;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import com.example.componentstool.utils.AppListManagerUtils;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    SharedPreferences sharedPreferences;
    EditText editTextAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取配置信息,
        Button check = findViewById(R.id.check);

        //获取SharedPrefences对象
        sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE);

        String address = sharedPreferences.getString("address", "http://192.168.1.151:8080");
        editTextAddress = findViewById(R.id.address);
        editTextAddress.setText(address);
        //检查权限
        check.setOnClickListener(this);
        //确定按钮
        findViewById(R.id.confirm).setOnClickListener(this);
        //权限检查按钮
        findViewById(R.id.check).setOnClickListener(this);
        //应用列表按钮
        findViewById(R.id.appList).setOnClickListener(this);

        //备份按钮
        findViewById(R.id.appBackups).setOnClickListener(this);
        //恢复按钮
        findViewById(R.id.appRecover).setOnClickListener(this);

        //清理按钮
        findViewById(R.id.appClear).setOnClickListener(this);

        //在线按钮
        findViewById(R.id.online).setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            //检查权限按钮
            case R.id.check:
                //检查Root
                //检查应用查看权限
//                String permission = "android.permission.GET_PACKAGE_SIZE";
                int result = ContextCompat.checkSelfPermission(this, Manifest.permission.QUERY_ALL_PACKAGES);
                if (result == PackageManager.PERMISSION_GRANTED) {
                    // 你已经具有读取应用列表的权限
                    Toast.makeText(this, "这是一条消息", Toast.LENGTH_SHORT).show();
//                    Toast.makeText(getApplicationContext(), "这是一条消息", Toast.LENGTH_SHORT).show();

                } else {
                    // 你没有读取应用列表的权限
                    //请自行到应用列表里面赋予其权限
                    Toast.makeText(this, "你没有权限..", Toast.LENGTH_SHORT).show();
                }

                break;
                //确认按钮
            case R.id.confirm:
                Editable text = editTextAddress.getText();
                SharedPreferences.Editor edit = sharedPreferences.edit();
                edit.putString("address", text.toString());
                edit.commit();
                break;

            //应用列表按钮
            case R.id.appList:
                AppListManagerUtils.printAppList( this.getPackageManager());


                break;

            //备份
            case R.id.appBackups:


                break;

            //恢复
            case R.id.appRecover:


                break;

            //清理
            case R.id.appClear:


                break;

            //在线
            case R.id.online:


                break;
        }
    }
}